import * as types from '../actions/types'
import createReducer from './createReducer'

const initState = {
	offset: 0,
	perPage: 18,
	pageCount: 0,
	hasNext: false
}

export const paginator = createReducer(initState,{
	[types.PAGINATE_SET_OFFSET](state, action){
		return Object.assign({}, state, {offset: action.offset});
	},
	[types.PAGINATE_SET_PAGECOUNT](state, action){
		return Object.assign({}, state, {pageCount: action.pageCount});
	},
	[types.SET_PAGINATION](state, action){
		let {offset, perPage} = state;
		const pageCount = Math.ceil(action.res.total / perPage)
		const hasNext = action.res.hasNext;
		offset += perPage
		return Object.assign({}, state, {pageCount, hasNext, offset});
	},
	[types.CLEAR_PAGINATION](state, action){
		return initState;
	},
})